package com.loop.unclelend.ui.presenter

import com.loop.unclelend.network.ApiService
import com.loop.unclelend.network.ApiService.addCommonParams
import com.loop.unclelend.network.ApiService.addRandomParams
import com.loop.unclelend.network.IHttpCallback
import com.loop.unclelend.pro.ProPresenter
import com.loop.unclelend.ui.view.IView_Launch
import com.loop.unclelend.ui.view.IView_Loan_Limit
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import com.loop.unclelend.utils.helper.CustomConfigsHelper
import com.loop.unclelend.utils.helper.CustomProcessHelper
import com.loop.unclelend.utils.helper.SharedHelper
import com.loop.unclelend.utils.helper.TestDataLibInvoke
import com.loop.unclelend.utils.permission.OnPermissionCallback
import com.loop.unclelend.utils.permission.XXPermissions
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/10 13:41
 * @Annotation：未授信、未借款、已借款
 */
class Pre_Loan_Limit(private val iView: IView_Loan_Limit): ProPresenter(iView) {

    var amountInfo: JSONObject? = null

    /**
     * 获取当前流程状态
     */
    fun checkUncleProcessSteps(){
        val stepList = CustomProcessHelper.getUncleStepList()
        if (stepList.size > 0){
            iView.hideUncleLoading()
            //未授信
            iView.showUncleUncreditedViews()
            return
        }
        //所有步骤已完成，获取额度信息
        requestLimitAmountAction()
    }

    /**
     * 获取额度信息
     */
    fun requestLimitAmountAction(){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("path", ApiService.API_PATH.UNCLE_API_LOAN_LIMIT_PATH)
        }
        doPostAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    amountInfo = obj
                    //已用额度
                    val utilisedAmount = obj.optInt("utilisedAmount")
                    if (utilisedAmount > 0){
                        //已借款
                        iView.showUncleAmountViews(obj)
                    }  else {
                        iView.showUncleSelectAmountViews(obj)
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hideUncleLoading()
            }
        })
    }

    /**
     * 检查权限是否全部申请
     */
    fun checkUncleAllPermissionsAgree(amount: String = ""){
        val permissions = CustomConfigsHelper.getUnclePermissionsList()
        if (XXPermissions.isGranted(iView.getAct(),permissions)){
            showUncleCheckPermissionSuccess(amount)
            return
        }

        //是否是审核账号
        val isConfigAccount = SharedHelper.getUncleSPBoolean(SharedHelper.UNCLE_SPKEY.UNCLE_IS_CONFIG_ACCOUNT)
        if(isConfigAccount) {//审核账号，可以进行申贷操作
            showUncleCheckPermissionSuccess(amount)
            return
        }

        XXPermissions.with(iView.getAct())
            // 申请多个权限
            .permission(permissions)
            .request(object: OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>, p1: Boolean) {
                    if (p1){
                        var permissionString = ""
                        permissions.forEach {
                            permissionString = "$permissionString$it,"
                        }
                        UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
                        showUncleCheckPermissionSuccess(amount)
                    }
                }
                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    var permissionString = ""
                    permissions.forEach {
                        permissionString = "$permissionString$it,"
                    }
                    UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))

                    XXPermissions.startPermissionActivity(iView.getAct(),permissions)
                }
            })
    }

    /**
     * 权限检查成功
     */
    private fun showUncleCheckPermissionSuccess(amount: String){
        //上报
        TestDataLibInvoke.sycData()

        //流程未完成 - 进入认证页面
        if (CustomProcessHelper.getUncleStepList().size > 0){
            iView.showUncleStepsPage()
            return
        }

        //流程已完成 - 进入申贷页面
        iView.showUncleLoanApplyPage(amount)
    }
}